package com.shenmazong.demoes00.task;

import com.shenmazong.demoes00.mapper.TbProductMapper;
import com.shenmazong.demoes00.pojo.TbProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.util.List;

@Slf4j
public class DbToEsThread extends Thread {

    private ElasticsearchRestTemplate _restTemplate;
    private TbProductMapper _mapper;

    public void setEsAndDb(ElasticsearchRestTemplate restTemplate, TbProductMapper mapper) {
        _restTemplate = restTemplate;
        _mapper = mapper;
    }

    @Override
    public void run() {
        //--1 从数据库中读取数据
        List<TbProduct> tbProducts = _mapper.selectList(null);

        //--2 写入ES中
        if(tbProducts != null) {
            _restTemplate.save(tbProducts);
        }

        log.info("导入es已经完成了......");
    }
}
